package com.xu.tao.demos.resultMap;

import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.junit.Before;
import org.junit.Test;

import java.io.IOException;
import java.io.InputStream;
import java.util.List;

public class App {

    private SqlSessionFactory sqlSessionFactory;

    @Before
    public void init() throws IOException {
        String resource = "mybatis/resultMap.xml";
        InputStream inputStream = Resources.getResourceAsStream(resource);
        sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
    }

    //ResultMap 高级映射   关联嵌套查询
    @Test
    public void t1(){
        SqlSession sqlSession = sqlSessionFactory.openSession();
        StudentMapper mapper = sqlSession.getMapper(StudentMapper.class);
        List<Grade> grade = mapper.selectGradeQuery(6);
        System.out.println(grade);
    }


    //ResultMap 高级映射   关联嵌套结果
    @Test
    public void t2(){
        SqlSession sqlSession = sqlSessionFactory.openSession();
        StudentMapper mapper = sqlSession.getMapper(StudentMapper.class);
        List<Grade> grade = mapper.selectGradeResult();
        System.out.println(grade);
    }


    //ResultMap 高级映射   关联嵌套查询_集合
    @Test
    public void t3(){
        SqlSession sqlSession = sqlSessionFactory.openSession();
        StudentMapper mapper = sqlSession.getMapper(StudentMapper.class);
        Student student1 = mapper.select1(6);
        System.out.println(student1);
    }

    @Test
    public void t4(){
        SqlSession sqlSession = sqlSessionFactory.openSession();
        StudentMapper mapper = sqlSession.getMapper(StudentMapper.class);
        Student student1 = mapper.select2(6);
        System.out.println(student1);
    }

}
